<? if (!defined("BASEPATH")) exit("No direct script access allowed"); ?>

<?
$onsubmit = "
						if(!\$('#loginError').length)
						$.ajax({
							type: 'POST',
							dataType: 'json',
							url: '".site_url()."login',
							data: $('#frmLogin').serialize(),
							beforeSend: function(xhr) {
								$('#modal').mask('".$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))."');
							},
							success: function(data) {
								if(data.success) {
									document.location.href = '".$this->input->post("redirect")."';
								} else {
									d=document.createElement('div');
									d.id = 'loginError',
									\$(d).addClass('alert_error')
										.html('".$this->db->escape_str($this->lang->line("WRONG_EMAIL_OR_PASSWORD"))."')
										.appendTo(\$('#modal'))
										.click(function(){
											\$(this).remove();
										})
										.hide()
										.slideToggle(300)
										.delay(2500)
										.slideToggle(300)
										.queue(function() {
											\$(this).remove();
										});
								}
							},
							async: false
						}).always(function() {\$('#body').unmask();});
						return false;
						";
?>

<div class="fl" style="width:300px; height: 300px; border-right: 1px solid grey; margin-right:20px;">
	<h2><?=$this->lang->line("SIGN_IN_WITH_YOUR_EXISTING_ACCOUNT")?></h2><br/>
		<?php
			if($this->session->flashdata("error")) {
				echo	"<div class=\"alert_error\">".$this->session->flashdata("error")."</div>";
			}
		?>
		<p><?=$this->lang->line("EXISTING_ACCOUNT_DESCRIPTION")?></p>

		<?
		echo	"<div onclick=\"location.href='".site_url()."openid/google';\" style=\"margin-left: auto; margin-right:auto; cursor:pointer; background-image:url(".MEDIA."icon/google/web_layer_0003s_0004_bg.png); width:190px; height: 36px;\">&nbsp;&nbsp;<img src=\"".MEDIA."icon/google/web_layer_0003s_0000_google.png\" alt=\"\" /><img src=\"".MEDIA."icon/google/web_layer_0003s_0001_line.png\" alt=\"\" /><img src=\"".MEDIA."icon/google/web_layer_0003s_0002_line.png\" alt=\"\" />&nbsp;&nbsp;<img src=\"".MEDIA."icon/google/web_layer_0003s_0003_Sign-in-with-Google+.png\" alt=\"\" /></div>";
		
		echo	"<div onclick=\"fb_login(); return false;\" style=\"margin-left: auto; margin-right:auto; cursor:pointer; background-image:url(".MEDIA."icon/facebook/web_layer_0001s_0004_bg.png); width:190px; height: 36px;\">&nbsp;&nbsp;<img src=\"".MEDIA."icon/facebook/web_layer_0001s_0000_facebook.png\" alt=\"\" style=\"margin-right:10px;\" /><img src=\"".MEDIA."icon/facebook/web_layer_0001s_0001_line.png\" alt=\"\" /><img src=\"".MEDIA."icon/facebook/web_layer_0001s_0002_line.png\" alt=\"\" />&nbsp;&nbsp;<img src=\"".MEDIA."icon/facebook/web_layer_0001s_0003_Sign-in-with-Facebook.png\" alt=\"\" /></div>";
		//echo	anchor(site_url()."openid/google", "<img src=\"".MEDIA."icon/Red-signin_Long_press_44dp.png\" alt=\"\" />");
		/*echo	anchor("", "<img src=\"".MEDIA."icon/Red-signin_Long_press_44dp.png\" alt=\"\" />", "onclick=\"
									$.ajax({type: 'POST', url: '".site_url()."openid/google', data:'redirect=".current_url()."', beforeSend: function(xhr) {\$('#body').mask('".$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))."');} }).
										always(function() {\$('#body').unmask();});
									return false;
								\"");*/
		?>
</div>

<?/*<div id="fb-root"></div>*/?>
<script type="text/javascript">
	window.fbAsyncInit = function() {
		FB.init({
			appId   : '<?=$this->config->item("APP_ID")?>',
			channelUrl : '//<?=site_url()?>/channel.html', 
			oauth   : true,
			status  : true, // check login status
			cookie  : true, // enable cookies to allow the server to access the session
			xfbml   : true // parse XFBML
		});

	  };

	function fb_login(){
		FB.login(function(response) {

			if (response.authResponse) {
				//console.log('Welcome!  Fetching your information.... ');
				//console.log(response); // dump complete info
				access_token = response.authResponse.accessToken; //get access token
				user_id = response.authResponse.userID; //get FB UID

				FB.api('/me', function(response) {
					$.ajax({type: 'POST', url: '<?=site_url()?>facebook_login', data: $.param(response), beforeSend: function(xhr) {$('#modal').mask('<?=$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))?>');} }).
						always(function() { /*$('#modal').unmask();*/ location.href='<?=site_url()?>';});
				});

			} else {
				//user hit cancel button
				//console.log('User cancelled login or did not fully authorize.');

			}
		}, {
			scope: 'email'
		});
	}
	(function() {
		var e = document.createElement('script');
		e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
		e.async = true;
		document.getElementById('fb-root').appendChild(e);
	}());
</script>

<div class="fl" style="width:450px;  height: 300px;">
	<h2><?=str_replace("{SITE_NAME}", $this->config->item("siteName"), $this->lang->line("SING_IN_WITH_YOUR_SITE_NAME_ACCOUNT"))?></h2><br/><br/>
	<?=form_open("login", array("id" => "frmLogin", "onsubmit" => $onsubmit))?>
		<div class="fl">
		<?=form_input(array("name" => "usrEmail", "id" => "usrEmail", "tabindex" => 1, "class" => "form_data data w100", "style" => "width:315px; padding-right: 150px;")).form_input(array("class" => "inlinebutton", "style" => "margin-left: -150px;", "type" => "button", "value" => $this->lang->line("E-MAIL_ADDRESS")))?>
		</div>
		<br class="cb" /><br class="cb" />
		<div class="fl">
			<?=form_password(array("name" => "usrPassword", "id" => "usrPassword", "tabindex" => 2, "class" => "form_data data w100", "style" => "width:315px; padding-right: 150px;", "value" => "")).form_input(array("class" => "inlinebutton", "style" => "margin-left: -150px;", "type" => "button", "value" => $this->lang->line("PASSWORD")))?>
		</div>
		<br class="cb" /><br class="cb" />

		<?
		echo	"<div class=\"fl\">".form_checkbox(array("name" => "input_rememberme", "value" => "checked", "id" => "input_rememberme"))." <span style=\"position:relative; top:-2px;\">".form_label($this->lang->line("REMEMBER_ME"), "input_rememberme")."</span>";
		echo	"<br/><br/>".anchor("", $this->lang->line("FORGOT_PASSWORD"), "onclick=\"
							$.ajax({type: 'POST', url: '".site_url()."forgot_password_form', data:'redirect=".current_url()."', beforeSend: function(xhr) {\$('#body').mask('".$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))."');} }).
								always(function() {\$('#body').unmask();});
							return false;
						\"")."</div>";
		?>

		<div class="fr">
			<?=form_hidden("redirect", $this->input->post("redirect"))?>
			<?=form_input(array("class" => "button", "tabindex" => 3, "style" => "margin-top:-2px;", "type" => "submit", "value" => $this->lang->line("LOGIN")))?>
		</div><br/>
		<br class="cb" /><br class="cb" />

	<?=form_close()?>
</div>
<br class="cb" />